package com.shangcheng.goods.controller;

import com.github.pagehelper.Page;
import com.shangcheng.goods.service.BrandService;
import com.zhouyangyang.entity.PageResult;
import com.zhouyangyang.entity.Result;
import com.zhouyangyang.entity.StatusCode;
import com.zhouyangyang.goods.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.shangcheng.goods.controller
 * @ClassName: BrandController
 * @Author: zhouyangyang
 * @Date: 2020/7/26 0:07
 * @Description:
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌
     * @return brandList
     */
    @GetMapping
    public Result<Brand> findAllBrand(){
        List<Brand> brandList = brandService.findAll();
        return new Result<Brand>(true,StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return brand
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@NotNull @PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true,StatusCode.OK,"查询成功",brand);
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping
    public Result addBrand(@RequestBody Brand brand){
        brandService.addBrand(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping
    public Result updateBrand(@RequestBody Brand brand){
        brandService.upDate(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除品牌
     * @param id 品牌id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteBrand(@NotNull @PathVariable("id") Integer id){
        brandService.deleteBrand(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 多条件查询
     * @param SearchMap 条件
     * @return
     */
    @GetMapping("/query")
    public Result queryByParam(@RequestParam Map<String,Object> SearchMap){
        List brandList = brandService.findList(SearchMap);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/query/{page}/{size}")
    public Result queryToPage(@NotNull @PathVariable("page") Integer page, @NotNull @PathVariable("size") Integer size){
        Page<Brand> brandPage = brandService.findToPage(page, size);
        PageResult pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 条件分页查询
     * @param searchMap 查询条件
     * @param page 当前页
     * @param size 每页显示条数
     * @return
     */
    @GetMapping("/queryByCondition/{page}/{size}")
    public Result queryToPage (@RequestParam Map<String,Object> searchMap,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Brand> brandPage = brandService.findToPage(searchMap, page, size);
        PageResult pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param cateGoryName 分类名称
     * @return
     */
    @GetMapping("/cateGory/{cateGoryName}")
    public Result findListByCategoryName(@PathVariable("cateGoryName")String cateGoryName){
        List<Map> listByCateGoryName = brandService.findListByCateGoryName(cateGoryName);
        return new Result(true,StatusCode.OK,"查询成功",listByCateGoryName);
    }
}
